package pong.rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import pong.Main;
import pong.lan.Lan;

public class ComRemotaImpl extends UnicastRemoteObject implements ComRemota{

	private static final long serialVersionUID = -2684257150273289629L;
	private String gid;
	private boolean enJuego;
	
	public ComRemotaImpl() throws RemoteException {
		super();
		gid = java.util.UUID.randomUUID().toString();
	}

	@Override
	public void pruebaConexion() throws RemoteException {}

	@Override
	public String getUUID() throws RemoteException {
		return gid;
	}

	@Override
	public void add(String hostname, ComRemota host) throws RemoteException {
		Lan.add(host, hostname);
	}

	@Override
	public String getNombreEnLista() throws RemoteException {
		synchronized (this) {
			if(enJuego) return Main.username.getText();
			return null;
		}
	}

	@Override
	public void setGame(boolean b) throws RemoteException {
		synchronized (this) {
			enJuego = b;
		}
	}

	@Override
	public void actualizarLista() throws RemoteException {
		synchronized (Lan.lock) {
			Lan.actualizarLista();
		}
	}

	@Override
	public boolean irJuego(ComRemota host) throws RemoteException {
		synchronized (this) {
			if(enJuego){
				Main.irJuego(host, false);
				enJuego = false;
				return true;
			}else{
				return false;
			}
		}
	}

	@Override
	public String getUsername() throws RemoteException {
		return Main.username.getText();
	}

	@Override
	public void abajo() throws RemoteException {
		Main.pong.abajo1();
	}

	@Override
	public void arriba() throws RemoteException {
		Main.pong.arriba1();
	}

}
